﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using FShop_BAL;
using FShop_DAL;
public partial class EditMember : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if ( !IsPostBack )
        {
            string member_ID = Request.QueryString["User_ID"];
            if (ExtendFunctional.IsNumberic(member_ID) == false)
            {

                //if (Session["User_Type"].ToString() != "2")
                //{
                //    MessageBox.Show("Bạn không có quyền.");
                //    Response.Redirect("ManageCategory.aspx");
                //}
                SetControlForInsert();
            }
            else
            {
                LoadControlForEdit(member_ID);
                
            }
        }
        else
        {
        
        }
    }
	#region For Set control when edit/insert
    private void SetControlForInsert()
    {
        btnAdd.Visible = true;
        btnEdit.Visible = false;
        btnChangePass.Visible = false;
        lblMK.Text = "Mật khẩu :";
        lblMK.Visible = true;
        //txtMK.Visible = true;
        //btnBack.Attributes.Add("onclick", "javascript :history.back(1)");
    }

    private void LoadControlForEdit(string member_ID)
    {
        User user = User_BAL.Get_User_ByID(int.Parse(member_ID));
        if (user != null)
        {           
			//Gan control voi cac truong trong doi tuong
            txtAddress.Value = user.User_Address;
            //txtBirthday.Text = user.User_Birthday.ToString();
            txtCompany.Value = user.User_Company;
            txtEmail.Value = user.User_Email;
            txtUser_Name.Value = user.User_Name;            
            txtNick.Value = user.User_Nick;
            rdiNam.Checked = user.User_Sex;
            rdiNu.Checked = !user.User_Sex;
            ddlGroup.SelectedIndex = (int)user.User_Type;
            txtPhone.Value = user.User_Phone;            

        }
        btnAdd.Visible = false;
        btnEdit.Visible = true;
        btnChangePass.Visible = true;
        lblMK.Visible = false;
        txtMK.Visible = false;
    }
    #endregion
	#region Insert/Update and upload image
    protected void Update()
    {
        string member_ID = Request.QueryString["User_ID"];
        User user = User_BAL.Get_User_ByID(int.Parse(member_ID));
		if (user != null)
        {
            user.User_Address= txtAddress.Text;

            user.User_Company = txtCompany.Text;
            user.User_Email = txtEmail.Text;
            user.User_Name = txtUser_Name.Text;
            user.User_Nick = txtNick.Text;
            
            user.User_Sex=rdiNam.Checked;
            user.User_Type =byte.Parse( ddlGroup.SelectedValue.ToString());
            user.User_Phone = txtPhone.Text;        
			User_BAL.Update_User(user);
			
		}
        
    }
    protected void Insert()
    {
        User user = new User();
		//Gan du lieu vao doi tuong		
        user.User_Address = txtAddress.Text;
        //user.User_Birthday = Convert.ToDateTime(txtBirthday.Text);
        user.User_Company = txtCompany.Text;
        user.User_Email = txtEmail.Text;
        user.User_Name = txtUser_Name.Text;
        user.User_Nick = txtNick.Text;        
        user.User_Sex = rdiNam.Checked;
        user.User_Type = byte.Parse(ddlGroup.SelectedValue.ToString());
        user.User_Phone = txtPhone.Text;
        //user.User_CreateDate = DateTime.Now;
        user.User_Password = CryptorEngine.Encrypt(txtMK.Text.Trim(), true);
		//Them moi
		User_BAL.Insert_User(user);		             

    }

   
    #endregion
	#region Button event
     protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Insert();
			MessageBox.Show("Đã thêm mới thành công.");
        }
        catch(Exception ex)
        {
			MessageBox.Show("Thêm mới không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Update();
			MessageBox.Show("Đã cập nhật thành công.");
        }
        catch (Exception ex)
        {
			MessageBox.Show("Cập nhật không thành công do có lỗi xảy ra.");
            throw ex;
        }
    } 
	#endregion
    protected void btnAcceptChange_Click(object sender, EventArgs e)
    {
        if(txtPass.Text.Length<=0 || txtPass2.Text.Length<=0 )
        {
            MessageBox.Show("Password không được rỗng.");
            txtPass.Focus();
            return ;
        }
        if(txtPass.Text.Trim()!= txtPass2.Text.Trim())
        {
            MessageBox.Show("Password không trùng nhau.");
            txtPass.Focus();
            return ;
        }
        string member_ID = Request.QueryString["User_ID"];

        User user = User_BAL.Get_User_ByID(int.Parse(member_ID));
        if (user != null)
        {
            
            user.User_Password =CryptorEngine.Encrypt( txtPass.Text.Trim(),true);
            User_BAL.Update_User(user);
            MessageBox.Show("Password đã được thay đổi.");
        }
    }
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageMember.aspx");
    }

}

